/**
 * Get local IP Address
 */
var ip_address = (function getIPAdress() {
    var interfaces = require('os').networkInterfaces();
    for (var devName in interfaces) {
        var iface = interfaces[devName];
        for (var i = 0; i < iface.length; i++) {
            var alias = iface[i];
            if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
                return alias.address;
            }
        }
    }
})();
/**
 * Grunt Configuration
 */

let static_declaratory = 'public';

module.exports = function (grunt) {

    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        less: {
            compileCore: {
                src: 'src/less/all.less',
                dest: static_declaratory +'/css/style.css'
            },
            options:{
                compress:true
            }
        },
        watch: {
            scripts: {
                files: ['src/less/*.less', 'src/pug/**/*.pug', static_declaratory+'/js/*.js', static_declaratory+'/js/*.json', static_declaratory+'/css/*.css'],
                tasks: ['less', 'pug','autoprefixer']
            },
            options: {
                spawn: false,
                livereload: true,
            }
        },
        connect: {
            base: {
                options: {
                    port: 3008,
                    hostname: ip_address,
                    livereload: true,
                    open: true
                }
            },
            keepalive: {
                options: {
                    port: 3008,
                    hostname: ip_address,
                    livereload: true,
                    keepalive: true,
                    open: true
                }
            }
        },
        pug: {
            compile: {
                options: {
                    pretty: true,
                    data: {
                        debug: false
                    }
                },
                files: {
                    'public/index.html': ['src/pug/index.pug'],
                }
            }
        },
        autoprefixer: {
            all: {
                options: {
                    map: false
                },
                src: static_declaratory+'/css/style.css',
                dest: static_declaratory+'/css/style.css'
            }
        }
    });

    grunt.loadNpmTasks('grunt-contrib-less');
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-contrib-concat');
    grunt.loadNpmTasks('grunt-contrib-connect');
    grunt.loadNpmTasks('grunt-contrib-copy');
    grunt.loadNpmTasks('grunt-contrib-pug');
    grunt.loadNpmTasks('grunt-autoprefixer');
    grunt.loadNpmTasks('grunt-contrib-cssmin');

    grunt.registerTask('default', ['connect:base', 'less', 'pug', 'autoprefixer', 'watch']);

};